package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ExamReviewFrag;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.utils.ToastUtil;
import com.konne.nightmare.FastPublicOpinion.widget.QJActionBar;
import com.konne.nightmare.FastPublicOpinion.widget.QJCheckBox;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/2/16  Time:下午 6:01.
 * description: 考试用户打回或者催交 .
 */
public class ExamEditAct extends BaseMvpActivity {
    @BindView(R.id.tv_sel_num)
    TextView tv_sel_num;
    @BindView(R.id.cb_all_sel)
    QJCheckBox cb_all_sel;
    private int type;
    /**
     * @param type 0,打回。1催缴
     * */
    public static void start(int type){
        Intent intent=new Intent(ActivityUtils.getTopActivity(),ExamEditAct.class);
        intent.putExtra("type",type);
        ActivityUtils.startActivity(intent);
    }
    @Override
    protected BaseMvpPresenter createPresenter() {
        return new BaseMvpPresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.act_exam_edit;
    }

    @Override
    public void initView() {
        type=getIntent().getIntExtra("type",0);
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this,true);
        QJActionBar actionBar=findViewById(R.id.action_bar);
        SuperTextView stv_submit=findViewById(R.id.stv_submit);
        stv_submit.setText(type==0?"打回":"催交");
        actionBar.setTitle(type==0?"打回":"催交");
        if(type==0){
            stv_submit.setSolid(0xffF8475F);
            stv_submit.setShaderEnable(false);
        }
        RecyclerView recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        EditAdapter mAdapter=new EditAdapter(testData());
        recyclerView.setAdapter(mAdapter);
        findViewById(R.id.stv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtil.showSuccessToast(type==0?"打回成功":"催交成功");
            }

        });
        cb_all_sel.setOnCheckedChangeListener(new QJCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJCheckBox var1, boolean var2) {
                    mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }
    private List<Remove> testData() {
        List<Remove> datas = new ArrayList<>();//模拟数据
        datas.add(new Remove(3));
        datas.add(new Remove(4));
        datas.add(new Remove(2));
        datas.add(new Remove(1));
        datas.add(new Remove(0));
        datas.add(new Remove(0));
        datas.add(new Remove(0));
        return datas;
    }
    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }
    Set<Remove> selSet=new ArraySet<>();

    class EditAdapter extends BaseQuickAdapter<Remove, BaseViewHolder>{
        public EditAdapter(@Nullable List<Remove> data) {
            super(R.layout.item_exam_edit,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Remove item) {
            QJCheckBox checkBox=helper.getView(R.id.checkbox);
            checkBox.setOnCheckedChangeListener(null);
            checkBox.setChecked(cb_all_sel.isChecked()?true:selSet.contains(item));
            checkBox.setOnCheckedChangeListener(new QJCheckBox.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(QJCheckBox var1, boolean var2) {
                    if(var2){
                        selSet.add(item);
                    }else {
                        if(selSet.contains(item)){
                            selSet.remove(item);
                        }
                        if(cb_all_sel.isChecked()){
                            cb_all_sel.setChecked(false);
                        }
                    }
                    tv_sel_num.setText("已选："+selSet.size());
                }
            });
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkBox.setChecked(!checkBox.isChecked());
                }
            });
            ImageView ic_avatar = helper.getView(R.id.ic_avatar);
            GlideUtils.loadCircleImage(item.avatar, ic_avatar);
            helper.setText(R.id.tv_name, item.name)
                    .setVisible(R.id.tv_sub_title,type==0);
            TextView tv_state=helper.getView(R.id.tv_state);

            if(type==0){
                helper.setText(R.id.tv_sub_title,String.format("提交时间:%s 用时:%s", item.time, item.useTime));
                if(item.type==2){
                    tv_state.setTextColor(0xffF9485D);
                    tv_state.setText("未批");
                }else {
                    SpanUtils.with(tv_state)
                            .append(item.score)
                            .setForegroundColor(0xff67ABED)
                            .setFontSize(20,true)
                            .append("分")
                            .setForegroundColor(0xffB4C1CE)
                            .setFontSize(12,true)
                            .create();
                }
            }else {
                tv_state.setTextColor(0xff67ABED);
                tv_state.setText("未交");
            }
        }
    }
    public class Remove {
        String avatar = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202011%2F20%2F20201120091707_fkBTd.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647593115&t=329837342300c79dc42ebd0ae9fa83f7";
        int type;
        String name = "ALAN";
        String time = "21-3-21 12:23";
        String useTime = "00:00:10";
        String score = "18";

        public Remove(int type) {
            this.type = type;
        }
    }
}
